package com.jd.fruit.controller.mobile;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.jd.fruit.common.annotation.Controller;
import com.jd.fruit.common.controller.BaseController;
import com.jd.fruit.common.util.DictUtil;
import com.jd.fruit.model.admin.shop.Goods;
import com.jd.fruit.model.admin.shop.GoodsAttribute;
import com.jd.fruit.model.admin.shop.GoodsSku;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * 商品购买
 * @author lishunfeng
 *
 */
@Controller("/mobile/goods")
public class MobileGoodsController extends BaseController{
	
	/**
	 * 默认访问页面
	 */
	public void index(){
		this.render("/mobile/goods/goods_list.html");
	}
	
	/**
	 *商品列表页面
	 */
	public void list(){
		Goods goods=this.getModel(Goods.class);
		int pageNumber = this.getParaToInt(0,1);
		Page<Record> goodss = Goods.dao.list(pageNumber,DictUtil.default_pageSize,goods);
		this.setAttr("goodss", goodss);
		this.setAttr("goods", goods);
		this.render("/mobile/goods/goods_list.html");
	}
	
	/**
	 *商品详情页面
	 */
	public void detail(){
		String goodsNo=this.getPara(0);  
		//查询商品
		Goods goods = Goods.dao.findByGoodsNo(goodsNo);
		//查询商品对应属性
		List<GoodsAttribute> goodAttrs=GoodsAttribute.dao.attrListByGoodsId(goods!=null?goods.getStr("id"):"");
		//查询商品属性对应值
		List<GoodsAttribute> goodAttrValues=GoodsAttribute.dao.listByGoodsId(goods!=null?goods.getStr("id"):"");
		//查询商品属性对应值
		List<GoodsSku> goodsSkus=GoodsSku.dao.listImage(goods!=null?goods.getStr("id"):"");
		this.setAttr("goods", goods);
		this.setAttr("goodAttrs",goodAttrs);
		this.setAttr("goodsSkus", goodsSkus);
		this.setAttr("goodAttrValues",goodAttrValues);
		this.setAttr("oprtype", DictUtil.oprUpdate);
	    this.render("/mobile/goods/goods_detail.html");
	}
	
	/**
	 * 商品验证库存
	 */
	public void getStockPrice(){
		String goodsId=this.getPara("goodsId");
		String attrStr=this.getPara("attrStr");
		String attrVal=GoodsSku.dao.goodsAttrVal(goodsId,attrStr);
		this.renderJson(attrVal);
	}
	
}
